package filereaders;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


/**
 * Creates a parser capable of reading tab
 * delimited files based on their columns.
 * 
 * @author Paul Dannenberg
 * 
 */
public class TxtColumnParser extends Parser {

    /**
     * Creates an object to read a specified 
     * file.
     * @param file
     */
    public TxtColumnParser(File file) {
        super(file);
    }

    /**
     * Creates an object to read a specified 
     * file.
     * @param scanner
     */
    public TxtColumnParser(Scanner scanner) {
        super(scanner);
    }

    /**
     * Reads a particular column.
     */
    @Override
    protected List<String> readSector(int dataIndex) {
        List<String> dataSector = new ArrayList<String>();
        int columnIndex = dataIndex;
        Scanner fileScanner = loadScanner();
        while (fileScanner.hasNextLine()) {
            String line = fileScanner.nextLine();
            Scanner lineScanner = new Scanner(line);
            while (lineScanner.hasNext()) {
                String dataPiece = lineScanner.next();
                if (columnIndex == 0) {
                    dataSector.add(dataPiece);
                }
                columnIndex--;
            }
            columnIndex = dataIndex;
        }
        return dataSector;
    }

    @Override
    public List<String> getOptions() {
        Parser rowParser = new TxtRowParser(loadScanner());
        return rowParser.readSector(0);
    }
}
